<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Request;

class Usersignin extends Api
{

    private static $userSignModel;
    protected $noNeedRight = '*';

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$userSignModel = new \app\common\model\UserSignin();
    }

    /**
     * 今日是否已签到
     */
    public function isSigned()
    {
        $userId = $this->auth->getUser()->id;
        $ret = self::$userSignModel->isSigned($userId);

        $this->success('今日是否已签到，true是false否', $ret);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $userId = $this->auth->getUser()->id;
        if (self::$userSignModel->isSigned($userId)) {
            $this->error('今日已签到！');
        }

        if (!self::$userSignModel->signed($userId)) {
            $this->error('签到失败！');
        }

        // 签到赠送积分
//        $days = self::$userSignModel->getSignedDays($userId);
        $site = config('site');
        $score = $site['signin_score'];

        $user = \app\common\model\User::find($userId);

        Db::startTrans();
        try {

            // 签到赠送积分
            $user->score += $score;
            $user->sign_days += 1;
            $user->save();
            $before = $user->score - $score;
            $sdata = [
                'user_id' => $userId,
                'record_type' => 1,
                'score' => $score,
                'before' => $before,
                'after' => $user->score,
                'memo' => '签到赠送积分'
            ];

            \app\common\model\UserScoreLog::create($sdata);

            $sign_days = $site['sign_days'];
            $gold_sign_days_nums = $site['gold_sign_days_nums'];
            // 周期满了奖励积分
            if ($user->sign_days >= $sign_days) {
                $user->sign_days = 0;
                $user->score += $gold_sign_days_nums;
                $user->save();
                $before = $user->score - $gold_sign_days_nums;
                $sdata = [
                    'user_id' => $userId,
                    'record_type' => 1,
                    'score' => $score,
                    'before' => $before,
                    'after' => $user->score,
                    'memo' => '签到额外奖励积分'
                ];

                \app\common\model\UserScoreLog::create($sdata);
            }

            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error('赠送积分失败');
        }

        $this->success('签到成功！');
    }

    /**
     * 连续签到天数
     */
//    public function continueSignedDays()
//    {
//        $userId = $this->auth->getUser()->id;
//        $days = self::$userSignModel->getSignedDays($userId);
//
//        $this->success('连续签到天数', $days);
//    }

    /**
     * 按周查找签到记录
     */
    public function signedDaysByWeek()
    {
        $userId = $this->auth->id;

        $data = self::$userSignModel->getSignedDaysByWeek($userId);
        $this->success('本周签到记录', $data);
    }
}
